package com.foogui.bootrabbitmq.delay;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 延迟消息消费服务
 *
 * @author wangxin213
 * @date 2024/04/05
 */
@Service
@Slf4j
public class DelayConsumerListener {
    /**
     * 必须绑定的是死信交换机
     *
     * @param jsonMsg json消息
     */
    @RabbitListener(queues = DelayConfig.DEAD_LETTER_5S_QUEUE_NAME)
    public void consume5SMsg(String jsonMsg, Channel channel,@Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        try {
            System.out.println("DelayConsumerListener.consume5SMsg收到的消息为: " + jsonMsg);
        } catch (Exception e) {
            log.info("消费失败落库");
        } finally {
            try {
                channel.basicAck(tag,false);
            } catch (IOException e) {
                log.info("签收异常");
            }
        }
    }
}
